import {User} from '../user/user-model.js';
import {Detail} from '../goodsDetail/detail-model.js';
import {Order} from '../orderOk/order-model.js';
var detail = new Detail();
var user = new User();
var order = new Order();
var app = getApp();
Page({
  data: {
    active: 0,
  },
  onLoad(options) { 
    this.setData({
      uid:app.globalData.uid,
      activeTab:options.tabIndex
    })
    this._loadData(options.tabIndex);
     
  },

  _loadData:function(type)
  {
    var that=this;
      my.request({
            url: "https://www.qqzuji.com/api/order/type?type="+type+"&uid="+app.globalData.uid,
            method:"get",
            headers:{
              'content-type': 'application/json',
            },
            success:function(res)
            {
                 that.setData({
                  orderArr:res.data.data,
                  active:type
                })
            },
            fail: function (err) {
            
            }
      })


  },

 
 showorder:function(e){
   
   var that=this;
    my.request({
            url: "https://www.qqzuji.com/api/order/type?type="+e.target.dataset.id+"&uid="+app.globalData.uid,
            method:"get",         
            headers:{
              'content-type': 'application/json',
            },
            success:function(res)
            {          
                that.setData({
                  active:e.target.dataset.id,
                  orderArr:res.data.data,              
                })
            },
            fail: function (err) {
            
            }

});

 },
 
 
  handleTabChange:function(e){


var that=this;
 my.request({
            url: "https://www.qqzuji.com/api/order/type?type="+e.index+"&uid="+app.globalData.uid,
            method:"get",         
            headers:{
              'content-type': 'application/json',
            },
            success:function(res)
            {          
                that.setData({
                  orderArr:res.data.data,
                  activeTab:Number(e.index)
                })
            },
            fail: function (err) {
            
            }

});

            

},


  //取消订单
  cancel:function(e)
  {
  var id = e.currentTarget.dataset.id;
   my.confirm({
     title:"温馨提示",
     content:"你确定要取消吗",
     confirmButtonText: '确认取消',
     cancelButtonText: '暂不取消',
     success: (res) => {
      if(res.confirm)
      {
           user.cancelOrder(id,(data)=>{
          if(data.code ==1)
          {
            my.showToast({
               type:'none',
               duration:1000,
               content:"取消成功",
               success: (res) => {
                this._loadData(this.data.activeTab)
              },
            });
          }
       })
      }
     
     },
   });
  },


  //查看账单
  showrent:function(e)
  {
    var id = e.currentTarget.dataset.id;
    my.navigateTo({
      url:'/pages/detailOrder/detailOrder?id='+id
    });
  },
  //确认收货
  confirm:function(e)
  {
    var id = e.currentTarget.dataset.id;
    user.confirmOrder(id,(data)=>{
      if(data.code ==1)
      {
        my.showToast({
          type:'none',
          content:"确认收货成功",
          success: (res) => {
            this._loadData(this.data.tabIndex)
          },
        });
      }
    })  
  },
  
  //租完即送
  rentsend:function(e)
  {
    var id = e.currentTarget.dataset.id;
    user.buyProductAfterOrder(id,(data)=>{
        var orderId = data.data.order_id;
        var orderNo = data.data.order_no;
        this.payOrder(orderId,orderNo);
    })
  },

  
  //复制物流单号
  express:function(e)
  {
      my.setClipboard({
      text: e.currentTarget.dataset.no,
      success: ( res ) => {
        my.showToast({
          content:"复制成功"
        });
      },
    });
  },
});
